//
//  SuperviseHomeVC.swift
//  SBSafetyApp
//
//  Created by Aimeow on 2022/11/22.
//

import Foundation
import UIKit

class SuperviseHomeVC: SubLevelViewController {
    let tableView = UITableView(frame: .zero, style: .plain)

    let datas: [SuperviseHomeModel] = [
        SuperviseHomeModel(type: .newTask, icon: "ic_sp_newtask", title: "新建任务"),
        SuperviseHomeModel(type: .task, icon: "ic_sp_task", title: "任务列表"),
        SuperviseHomeModel(type: .arrage, icon: "ic_sp_arrage", title: "待整改"),
        SuperviseHomeModel(type: .reCheck, icon: "ic_sp_arrage", title: "待复查"),
//        SuperviseHomeModel(icon: "ic_locate", title: "隐患跟踪"),
//        SuperviseHomeModel(type: .statitics, icon: "ic_asistance", title: "统计"),
    ]
    
    var rwkssj: String?
    var rwjssj: String?
    
    var modal: SpHomeModal?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        view.backgroundColor = .bg
        backBtn.tintColor = .white
        setupUI()
        
        if isSel == false {
//            let quarter = Date.getCurrentQuarter(month: date.momentMonth())
//            switch dateEnum {
//            case .daily:
//                rwkssj = date
//                rwjssj = date
//                break
//            case .monthly:
//                rwkssj = Date.startOfMonth(date: date.moment()).elTodayDate()
//                rwjssj = Date.endOfMonth(date: date.moment()).elTodayDate()
//                break
//            case .quarterly:
//                rwkssj = "\(date.momentYear())-\((quarter - 1) * 3 + 1)-01"
//                rwjssj = "\(date.momentYear())-\(quarter * 3)-31"
//                break
//            case .annual:
//                rwkssj = "\(date.momentYear())-01-01"
//                rwjssj = "\(date.momentYear())-12-31"
//                break
//            }
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.navBarStyle(.clear)
        reloadData()
    }
    
    func reloadData() {
        API.getJcjdHome(withParam: SpCommonParam(wddm: deptId, rwkssj: rwkssj, rwjssj: rwjssj)) { responseModel in
            DispatchQueue.main.async {
                self.modal = responseModel.model
                self.tableView.reloadData()
            }
        }
    }
    
    func setupUI() {
        let headImageView = UIImageView(image: UIImage.init(named: "supervise_head"))
        headImageView.contentMode = .scaleAspectFill
        view.addSubview(headImageView)
        headImageView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(252)
        }
        
        let titleView = SuperviseHomeTitleView()
        view.addSubview(titleView)
        titleView.snp.makeConstraints { make in
            make.top.equalTo(headImageView.snp.bottom).offset(24)
            make.centerX.equalTo(headImageView.snp.centerX)
            make.height.equalTo(20)
        }
        
        tableView.separatorStyle = .none
        tableView.showsVerticalScrollIndicator = false
        tableView.backgroundColor = .bg
        tableView.delegate = self
        tableView.dataSource = self
        view.addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(titleView.snp.bottom).offset(20)
            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom)
        }
    }
}

extension SuperviseHomeVC: UITableViewDataSource, UITableViewDelegate {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return datas.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let ID : String = "SuperviseHomeCell"
        var cell = tableView.dequeueReusableCell(withIdentifier: ID)
        if cell == nil {
            cell = SuperviseHomeCell(style: .subtitle, reuseIdentifier: ID)
        }
        let data = datas[indexPath.row]

        (cell as? SuperviseHomeCell)?.buildData(model: data)
        switch data.type {
        case .arrage:
            (cell as? SuperviseHomeCell)?.count = modal?.dzgs
            break
        case .reCheck:
            (cell as? SuperviseHomeCell)?.count = modal?.dfcs
            break
        default:
            (cell as? SuperviseHomeCell)?.count = 0
            break
        }
        return cell!
    }
    
//    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
//        return 110
//    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 90
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let data = datas[indexPath.row]
        switch data.type {
        case .newTask:
            navigationController?.pushViewController(SuperviseCreateTypeVC(), animated: true)
            break
        case .task:
            navigationController?.pushViewController(SuperviseTaskVC(withDeptId: deptId, deptName: deptName, rwkssj: rwkssj, rwjssj: rwjssj), animated: true)
            break
        case .arrage:
            navigationController?.pushViewController(SuperviseArrageVC(withDeptId: deptId, deptName: deptName, rwkssj: rwkssj, rwjssj: rwjssj), animated: true)
            break
        case .reCheck:
            navigationController?.pushViewController(SuperviseReCheckVC(withDeptId: deptId, deptName: deptName, rwkssj: rwkssj, rwjssj: rwjssj), animated: true)
            break
        case .statitics:
            break
        }
    }
}
